package com.zens.cms.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zens.cms.entity.TColumn;
import com.zens.cms.entity.TColumnExample;
import com.zens.cms.entity.TColumnExample.Criteria;
import com.zens.cms.mapper.TColumnMapper;
import com.zens.cms.services.IArticleSec;

@Service
public class ArticleSecService implements IArticleSec {
	
	@Resource
	TColumnMapper tColumnMapper;

	@Override
	public Map<String, List<Long>> copySourceForColumn(List<TColumn> columns,long websiteId,int lv,long oldcid,long group) {
		
		Map<Long, Long> colMap = new HashMap<Long,Long>();

		List<Long> newColidArr = new ArrayList<Long>();
		List<Long> oldColidArr = new ArrayList<Long>();
		long returnColid = 0,oldColid = 0;
		
		
		/*copy column*/
		TColumn _column;
		for(int i = 0,ilen = columns.size();i < ilen; i++){
			_column = columns.get(i);
			oldColid = _column.getId();
			
			_column.setId(null);
			_column.setFsiteid(websiteId);
			if(oldColid == oldcid){
				_column.setFcolumngp(group);
			}
			
			tColumnMapper.insertSelective(_column);
			returnColid = _column.getId();
			colMap.put(returnColid,oldColid);
			newColidArr.add(returnColid);
			oldColidArr.add(oldColid);
		}
		Iterator<Long> iter = colMap.keySet().iterator(); 
		Long colgp;
		while(iter.hasNext()){
			colgp = iter.next();
			TColumnExample te = new TColumnExample();
			Criteria tc = te.createCriteria();
			tc.andFcolumngpEqualTo(colMap.get(colgp));
			tc.andFsiteidEqualTo(websiteId);
			tc.andIdIn(newColidArr);
			TColumn column = new TColumn();
			column.setFcolumngp(colgp);
			tColumnMapper.updateByExampleSelective(column, te);
		}
		Map<String,List<Long>> resultMap = new HashMap<String,List<Long>>();
		resultMap.put("old", oldColidArr);
		resultMap.put("new", newColidArr);
		return resultMap;
	}

	@Override
	public List<TColumn> getColumnsByColids(List<Long> coidlist) {
		TColumnExample te = new TColumnExample();
		Criteria tc = te.createCriteria();
		tc.andIdIn(coidlist);
		return tColumnMapper.selectByExample(te);
	}

	@Override
	public int updateParentGroup(long sourid, long tarid,long webid) {
		TColumnExample te = new TColumnExample();
		Criteria tc = te.createCriteria();
		tc.andFcolumngpEqualTo(sourid);
		tc.andFsiteidEqualTo(webid);
		TColumn column = new TColumn();
		column.setFcolumngp(tarid);
		return tColumnMapper.updateByExampleSelective(column, te);
	}

}
